<template>
    <div>
        <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
            <el-form-item prop="username">
                <el-input v-model="param.username" clearable @clear="clear_password" placeholder="username">
                    <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                </el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password" placeholder="password" v-model="param.password" show-password @keyup.enter.native="submitForm()">
                    <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                </el-input>
            </el-form-item>
            <el-form-item>
                <div style="margin-top: 20px">
                    <el-radio-group v-model="param.radio" size="small">
                        <el-radio :label="1" border>用户</el-radio>
                        <el-radio :label="2" border>快递员</el-radio>
                        <el-radio :label="3" border>管理员</el-radio>
                    </el-radio-group>
                </div>
            </el-form-item>
            <el-form-item>
                <SliderCheck :successFun="handleSuccessFun" :errorFun="handleErrorFun"></SliderCheck>
            </el-form-item>
            <div class="login-btn">
                <el-button v-if="login_button" type="primary" disabled @click="submitForm()">登录</el-button>
                <el-button v-else type="primary" @click="submitForm()">登录</el-button>
            </div>
            <p class="login-tips">Tips : 验证通过，可点击登录按钮。</p>
        </el-form>
    </div>
</template>

<script>
import SliderCheck from './SliderCheck';
export default {
    components: { SliderCheck },
    data: function () {
        return {
            param: {
                username: '',
                password: '',
                radio: 1
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
            },
            login_button: true
        };
    },
    methods: {
        clear_password(){
            this.param.password = null;
        },
        // 点击登录按钮
        submitForm() {
            if (this.param.radio === 1) { 
                this.userLogin();
            } else if (this.param.radio === 2) {
                this.staffLogin();
            } else {
                this.adminLogin();
            }
        },
        userLogin() {
            this.$refs.login.validate((valid) => {
                if (valid) {
                    console.log(this.param.username);
                    let params = new URLSearchParams();
                    params.append('user_uname', this.param.username);
                    params.append('user_password', this.param.password);
                    // console.log(params);
                    const _this = this;
                    this.$axios({
                        method: 'post',
                        url: 'user/login',
                        data: params,
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' } //加上这个
                    })
                        .then(function (res) {
                            console.log(res);
                            if (res.data.code == 1) {
                                _this.$message.success('登录成功！');
                                localStorage.setItem('user_name', res.data.user.user_name);
                                // localStorage.setItem('user_uname', res.data.user.user_name);
                                localStorage.setItem('to_name', res.data.user.user_name);
                                localStorage.setItem('user_id', res.data.user.user_id);
                                localStorage.setItem('token', res.data.token);
                                localStorage.setItem('role', 'user');
                                _this.$router.push('/userDashBoard');
                            } else {
                                _this.$message.error('您输入的用户名或密码错误！');
                                console.log(_this);
                                return false;
                            }
                        })
                        .catch(function (error) {
                            console.log(_this.SliderCheck);
                            _this.$notify.error({
                                showClose: true,
                                title: '错误',
                                message: '服务器出错啦！'
                            });
                        });
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        staffLogin() {
            this.$refs.login.validate((valid) => {
                if (valid) {
                    console.log(this.param.username);
                    let params = new URLSearchParams();
                    params.append('staff_number', this.param.username);
                    params.append('staff_password', this.param.password);
                    const _this = this;
                    this.$axios({
                        method: 'post',
                        url: 'staff/login',
                        data: params,
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' } //加上这个
                    })
                        .then(function (res) {
                            console.log(res.data);
                            if (res.data.code == 1) {
                                _this.$message.success('登录成功');
                                localStorage.setItem('user_name', res.data.staff.staff_number);
                                localStorage.setItem('user_id', res.data.staff.staff_id);
                                localStorage.setItem('token', res.data.token);
                                localStorage.setItem('role', 'staff');
                                _this.$router.push('/staffDashBoard');
                            } else {
                                _this.$message.error('您输入的员工号或密码错误！');
                                return false;
                            }
                        })
                        .catch(function (error) {
                            _this.$notify.error({
                                showClose: true,
                                title: '错误',
                                message: '服务器出错啦！'
                            });
                        });
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        adminLogin() {
            this.$refs.login.validate((valid) => {
                if (valid) {
                    console.log(this.param.username);
                    let params = new URLSearchParams();
                    params.append('boss_uname', this.param.username);
                    params.append('boss_password', this.param.password);
                    const _this = this;
                    this.$axios({
                        method: 'post',
                        url: 'admin/login',
                        data: params,
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' } //加上这个
                    })
                        .then(function (res) {
                            console.log(res.data);
                            if (res.data.code == 1) {
                                _this.$message.success('登录成功');
                                localStorage.setItem('user_name', res.data.admin.boss_uname);
                                localStorage.setItem('user_id', res.data.admin.boss_id);
                                localStorage.setItem('token', res.data.token);
                                localStorage.setItem('role', 'admin');
                                _this.$router.push('/adminDashBoard');
                            } else {
                                _this.$message.error('您输入的用户名或密码错误！');
                                return false;
                            }
                        })
                        .catch(function (error) {
                            _this.$notify.error({
                                showClose: true,
                                title: '错误',
                                message: '服务器出错啦！'
                            });
                        });
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        // 滑块验证成功回调
        handleSuccessFun() {
            this.login_button = false;
        },
        // 滑块验证失败回调
        handleErrorFun() {}
    },
    created() {
        console.log(this.login_button2);
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: black;
}
</style>